package org.csu.mypetstorebackstage.service;

import org.csu.mypetstorebackstage.domain.Item;
import org.csu.mypetstorebackstage.domain.Product;
import org.csu.mypetstorebackstage.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.security.PublicKey;
import java.util.Iterator;
import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;

    //先别用这个
    public Product getProductById(String productId)
    {
        return productMapper.getProductById(productId);
    }

    public List<Product> getProductAllByCategory(String categoryName)
    {
        return productMapper.getProductAllByCategory(categoryName);
    }

    //用这个返回某个特定的product
    public Product getProductTest(String productName)
    {
        return productMapper.getProductTest(productName);
    }

    public List<Product> getProductAll()
    {
        return productMapper.getProductAll();
    }

    public List<Product> getProductListByCategory(String categoryId)
    {
        return productMapper.getProductListByCategory(categoryId);
    }
    public Product getProduct(String productId)
    {
        return productMapper.getProduct(productId);
    }

    public boolean addProduct(Product product)
    {
        return productMapper.addProduct(product);
    }
    public void updateProduct(Product product)
    {
        productMapper.updateProduct(product);
    }

    //删除商品时  所有对应的item均应删除
    @GetMapping("/deleteProduct")
    public void deleteProduct(String productId)
    {
//        List<Item>itemList=productMapper.getItemsByProductId(productId);
        List<Item>itemList=productMapper.getItemsByProductId(productId);
        Iterator<Item> itemIterator=itemList.iterator();
        while(itemIterator.hasNext())
        {
            productMapper.deleteItem(itemIterator.next().getItemId());
        }
        productMapper.deleteProduct(productId);
//        itemMapper.deleteItemListByItem(itemList);
//        List<Item >itemList=productMapper.getItemsByProductId(productId);
//        Iterator<Item>iterator =itemList.iterator();
//        while(iterator.hasNext())
//        {
//            Item item=iterator.next();
//            deleteItem(item.getItemId());
//        }
    }
}
